#include <nds.h>
#include <stdio.h>

#include "Sonic001_bin.h"
#include "bmpformat.h"

//---------------------------------------------------------------------------------
void setupVideoBackground(void)
{
        //set video mode to mode 5
        videoSetMode(MODE_5_2D);        
        //map vram a to start of background graphics memory
        vramSetBankA(VRAM_A_MAIN_BG);
        //initialize the background layer
        bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0,0);
}

//---------------------------------------------------------------------------------
void wipeScreen(u16 color)
{
	unsigned int ix, iy;
	// could probably use wmemset() here? leaving loops for readability
	for(iy = 0; iy < SCREEN_HEIGHT; iy++)
	{
		for(ix = 0; ix < SCREEN_WIDTH; ix++)
		{
			BG_GFX[iy * SCREEN_WIDTH + ix] = color;
		}
	}
}

 //---------------------------------------------------------------------------------
void uploadFakePalette(u16 *screen_palette)
{
	unsigned int i;

	for(i = 0 ; i < 4 ; i++)
	{
		screen_palette[4*i + 0] = RGB15(31,  0,  0);  // Red
		screen_palette[4*i + 1] = RGB15(31, 31,  0);  // Yellow
		screen_palette[4*i + 2] = RGB15( 0, 31,  0);  // Green
		screen_palette[4*i + 3] = RGB15( 0,  0, 31);  // Blue
	}

	for(i = 0 ; i < 4 ; i++)
	{
		screen_palette[16 + 4*i + 0] = RGB15(15,  0,  0);  // Dark Red
		screen_palette[16 + 4*i + 1] = RGB15(15, 15,  0);  // Dark Yellow
		screen_palette[16 + 4*i + 2] = RGB15( 0, 15,  0);  // Dark Green
		screen_palette[16 + 4*i + 3] = RGB15( 0,  0, 15);  // Dark Blue
	}
}

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	BmpFile *bmp;
	uint8_t choice;

	consoleDemoInit();
	setupVideoBackground();

	bmp = (BmpFile *)Sonic001_bin;
	choice = 0;

	validateBmp(bmp);

	while(1) 
	{
		scanKeys();
		if( keysHeld() & KEY_A)	choice = 0;
		if( keysHeld() & KEY_B)	choice = 1;

		uploadPalette(bmp, BG_PALETTE);
		if (choice == 0)
		{
			displayIndexedBmp(bmp, BG_GFX);
		}
		else
		{
			displayTestPattern(BG_GFX, 0, 16);
		}

	 	swiWaitForVBlank();
 	}
	return 0;
}
